package com.cxhc.system.service.app.impl;

import java.util.List;

import com.cxhc.common.utils.SecurityUtils;
import com.cxhc.system.domain.app.IntegralRecords;
import com.cxhc.system.mapper.SysUserMapper;
import com.cxhc.system.mapper.app.IntegralRecordsMapper;
import com.cxhc.system.service.app.IIntegralRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 积分获取（发放）记录Service业务层处理
 *
 * @author Dhq
 * @date 2024-09-03
 */
@Service
public class IntegralRecordsServiceImpl implements IIntegralRecordsService {
    @Autowired
    private IntegralRecordsMapper integralRecordsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询积分获取（发放）记录
     *
     * @param id 积分获取（发放）记录主键
     * @return 积分获取（发放）记录
     */
    @Override
    public IntegralRecords selectIntegralRecordsById(Long id) {
        return integralRecordsMapper.selectIntegralRecordsById(id);
    }

    /**
     * 查询积分获取（发放）记录列表
     *
     * @param integralRecords 积分获取（发放）记录
     * @return 积分获取（发放）记录
     */
    @Override
    public List<IntegralRecords> selectIntegralRecordsList(IntegralRecords integralRecords) {
        return integralRecordsMapper.selectIntegralRecordsList(integralRecords);
    }

    /**
     * 新增积分获取（发放）记录
     *
     * @param integralRecords 积分获取（发放）记录
     * @return 结果
     */
    @Override
    public int insertIntegralRecords(IntegralRecords integralRecords) {
        integralRecords.setOperationUserId(SecurityUtils.getUserId());
        integralRecords.setOperationUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        integralRecords.setBalance(integralRecords.getIntegral() + integralRecords.getBalance());
        int row = integralRecordsMapper.insertIntegralRecords(integralRecords);
        if (row > 0) {
            // 更新用户的积分余额
            sysUserMapper.updateBalance(integralRecords.getUserId(), integralRecords.getBalance());
        }
        return row;
    }

    /**
     * 批量删除积分获取（发放）记录
     *
     * @param ids 需要删除的积分获取（发放）记录主键
     * @return 结果
     */
    @Override
    public int deleteIntegralRecordsByIds(Long[] ids) {
        return integralRecordsMapper.deleteIntegralRecordsByIds(ids);
    }

    /**
     * 删除积分获取（发放）记录信息
     *
     * @param id 积分获取（发放）记录主键
     * @return 结果
     */
    @Override
    public int deleteIntegralRecordsById(Long id) {
        return integralRecordsMapper.deleteIntegralRecordsById(id);
    }
}
